package com.example.utils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class FileEncryptionUtilsImpl implements com.example.utils.FileEncryptionUtils {
    @Override
    public void encrypt(String filePath) {
        try {
            byte[] fileData = Files.readAllBytes(Paths.get(filePath));
            String encryptedData = Base64.getEncoder().encodeToString(fileData);
            Files.write(Paths.get(filePath + ".enc"), encryptedData.getBytes(StandardCharsets.UTF_8));
            System.out.println("文件加密成功: " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void decrypt(String filePath) {
        try {
            byte[] encryptedData = Files.readAllBytes(Paths.get(filePath));
            byte[] fileData = Base64.getDecoder().decode(new String(encryptedData, StandardCharsets.UTF_8));
            Files.write(Paths.get(filePath.replace(".enc", "")), fileData);
            System.out.println("文件解密成功: " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
